﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UploadManager;
using uCoreClasses;

namespace MBBStoreClient
{
    public partial class frmSettings : Form
    {
        uTesselation tes = new uTesselation("lowgate", 256);
        public frmSettings()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            SettingType[] st = new SettingType[3];
            st[0].Section = "LOCAL";
            st[0].Node = new NodeType[4];
            st[0].Node[0].Key = "Server";
            st[0].Node[0].Value = txtSvrL.Text;
            st[0].Node[1].Key = "Database";
            st[0].Node[1].Value = txtDbL.Text;
            st[0].Node[2].Key = "UID";
            st[0].Node[2].Value = txtUidL.Text;
            st[0].Node[3].Key = "PWD";
            st[0].Node[3].Value = tes.Tesselate(txtPwdL.Text);
            st[1].Section = "STOREDATA";
            st[1].Node = new NodeType[3];
            st[1].Node[0].Key = "StoreID";
            st[1].Node[0].Value = txtStoreID.Text;
            st[1].Node[1].Key = "StoreName";
            st[1].Node[1].Value = txtStoreName.Text;
            st[1].Node[2].Key = "StoreAddress";
            st[1].Node[2].Value = txtStoreAddress.Text;
            st[2].Section = "ONLINE";
            st[2].Node = new NodeType[4];
            st[2].Node[0].Key = "Server";
            st[2].Node[0].Value = txtSvrO.Text;
            st[2].Node[1].Key = "Database";
            st[2].Node[1].Value = txtDbO.Text;
            st[2].Node[2].Key = "UID";
            st[2].Node[2].Value = txtUidO.Text;
            st[2].Node[3].Key = "PWD";
            st[2].Node[3].Value = tes.Tesselate(txtPwdO.Text);
            Common.WriteSettings(st);
            try
            {
                st = Common.ReadSettings();

                Common.lServer = st[0].Node[0].Value;
                Common.lDatabase = st[0].Node[1].Value;
                Common.lUID = st[0].Node[2].Value;
                Common.lPWD = tes.Tesselate(st[0].Node[3].Value);

                Common.lStoreID = st[1].Node[0].Value;
                Common.lStoreName = st[1].Node[1].Value;
                Common.lStoreAddress = st[1].Node[2].Value;

                Common.oServer = st[2].Node[0].Value;
                Common.oDatabase = st[2].Node[1].Value;
                Common.oUID = st[2].Node[2].Value;
                Common.oPWD = tes.Tesselate(st[2].Node[3].Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read settings");
            }
            pnlForm.Dispose();
            this.Dispose();
        }






        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            SettingType[] st = new SettingType[2];
            try
            {
                st = Common.ReadSettings();
                txtSvrL.Text = st[0].Node[0].Value;
                txtDbL.Text = st[0].Node[1].Value;
                txtUidL.Text = st[0].Node[2].Value;
                txtPwdL.Text = tes.Tesselate(st[0].Node[3].Value);
                
                txtStoreID.Text = st[1].Node[0].Value;
                txtStoreName.Text = st[1].Node[1].Value;
                txtStoreAddress.Text = st[1].Node[2].Value;

                txtSvrO.Text = st[2].Node[0].Value;
                txtDbO.Text = st[2].Node[1].Value;
                txtUidO.Text = st[2].Node[2].Value;
                txtPwdO.Text = tes.Tesselate(st[2].Node[3].Value);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read settings");
            }
            
        }
    }
}
